package com.atguigu.mybatisplus;

import com.ctc.wstx.stax.WstxInputFactory;
import com.ctc.wstx.stax.WstxOutputFactory;
import com.fasterxml.aalto.stax.InputFactoryImpl;
import com.fasterxml.aalto.stax.OutputFactoryImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlElement;
import java.time.LocalDate;

/**
 * @author zhuzi
 */
public class XmlTest02 {
	@Test
	public void xml() throws JsonProcessingException, JAXBException {
		Person p1 = new Person("yitian", "易天", 25, "10000", LocalDate.of(1994, 1, 1));
		XmlFactory factory1 = new XmlFactory(new WstxInputFactory(), new WstxOutputFactory());

		XmlFactory factory = new XmlFactory(
				new InputFactoryImpl(), new OutputFactoryImpl());

		XmlMapper mapper = new XmlMapper();
		mapper.findAndRegisterModules();

		mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());
		mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		String text = mapper.writeValueAsString(p1);
		System.out.println(text);


	}

	static final String NAMESPACE = "http://jackson.test.model";

	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	// @JsonRootName(value = "Person")
	// @XmlRootElement(name = "OuterBean", namespace = NAMESPACE)
	@JacksonXmlRootElement(localName = "testRoot", namespace = NAMESPACE)
	public static class Person {
		@XmlElement(name = "name", namespace = NAMESPACE)
		private String name;
		// @JsonProperty("NickName")
		//@JacksonXmlText
		@XmlElement(name = "nickname", namespace = NAMESPACE)
		private String nickname;

		private int age;
		@XmlElement(name = "identityCode", namespace = NAMESPACE)
		private String identityCode;

		//@JacksonXmlProperty(isAttribute = true)
		@JsonFormat(pattern = "yyyy/MM/DD")
		@XmlElement(name = "birthday", namespace = NAMESPACE)
		private LocalDate birthday;

	}
}
